<!-- 历史数据编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改历史数据':'添加历史数据'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
                    
      <el-form-item
        label="设备编号:"
        prop="device_no">
        <el-input
          :maxlength="20"
          v-model="form.device_no"
          placeholder="请输入设备编号"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>
                              
      <el-form-item
        label="标况总量高位 m³:"
        prop="blHigh">
        <el-input
          :maxlength="20"
          v-model="form.blHigh"
          placeholder="请输入标况总量高位 m³"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="标况总量低位 m³:"
        prop="blLow">
        <el-input
          :maxlength="20"
          v-model="form.blLow"
          placeholder="请输入标况总量低位 m³"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="标况流量 m³/h:"
        prop="bkll">
        <el-input
          :maxlength="20"
          v-model="form.bkll"
          placeholder="请输入标况流量 m³/h"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="工况流量 m³/h:"
        prop="gkll">
        <el-input
          :maxlength="20"
          v-model="form.gkll"
          placeholder="请输入工况流量 m³/h"
          clearable/>
      </el-form-item>
                                              
      <el-form-item
        label="压力 kPa:"
        prop="yl">
        <el-input
          :maxlength="20"
          v-model="form.yl"
          placeholder="请输入压力 kPa"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="井下压力 单位 MPa:"
        prop="yali">
        <el-input
          :maxlength="20"
          v-model="form.yali"
          placeholder="请输入井下压力 单位 MPa"
          clearable/>
      </el-form-item>
                                              
      <el-form-item
        label="运行频率 Hz:"
        prop="pinlv">
        <el-input
          :maxlength="20"
          v-model="form.pinlv"
          placeholder="请输入运行频率 Hz"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="运行电压  V:"
        prop="voltage">
        <el-input
          :maxlength="20"
          v-model="form.voltage"
          placeholder="请输入运行电压  V"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="运行功率  KW:"
        prop="run_power">
        <el-input
          :maxlength="20"
          v-model="form.run_power"
          placeholder="请输入运行功率  KW"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="运行速度 RPM:"
        prop="run_speed">
        <el-input
          :maxlength="20"
          v-model="form.run_speed"
          placeholder="请输入运行速度 RPM"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="母线电压 V:"
        prop="mother_voltage">
        <el-input
          :maxlength="20"
          v-model="form.mother_voltage"
          placeholder="请输入母线电压 V"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="运行转矩 NM:"
        prop="run_len">
        <el-input
          :maxlength="20"
          v-model="form.run_len"
          placeholder="请输入运行转矩 NM"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="套压值 单位 MPa:"
        prop="tao_value">
        <el-input
          :maxlength="20"
          v-model="form.tao_value"
          placeholder="请输入套压值 单位 MPa"
          clearable/>
      </el-form-item>
                
      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'HistoryDataEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              device_no: [
          {required: true, message: '请输入设备编号', trigger: 'blur'}
        ],
                  
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
                          blHigh: [
          {required: true, message: '请输入标况总量高位 m³', trigger: 'blur'}
        ],
                  blLow: [
          {required: true, message: '请输入标况总量低位 m³', trigger: 'blur'}
        ],
                  bkll: [
          {required: true, message: '请输入标况流量 m³/h', trigger: 'blur'}
        ],
                  gkll: [
          {required: true, message: '请输入工况流量 m³/h', trigger: 'blur'}
        ],
                                    yl: [
          {required: true, message: '请输入压力 kPa', trigger: 'blur'}
        ],
                  yali: [
          {required: true, message: '请输入井下压力 单位 MPa', trigger: 'blur'}
        ],
                                    pinlv: [
          {required: true, message: '请输入运行频率 Hz', trigger: 'blur'}
        ],
                  voltage: [
          {required: true, message: '请输入运行电压  V', trigger: 'blur'}
        ],
                  run_power: [
          {required: true, message: '请输入运行功率  KW', trigger: 'blur'}
        ],
                  run_speed: [
          {required: true, message: '请输入运行速度 RPM', trigger: 'blur'}
        ],
                  mother_voltage: [
          {required: true, message: '请输入母线电压 V', trigger: 'blur'}
        ],
                  run_len: [
          {required: true, message: '请输入运行转矩 NM', trigger: 'blur'}
        ],
                  tao_value: [
          {required: true, message: '请输入套压值 单位 MPa', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/historydata/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
